﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
namespace Comp476Project
{
    class Champion:Minion
    {
        Primitives3D.SpherePrimitive sphere;
        public Champion(Primitives3D.SpherePrimitive sphere,Matrix proj, Model mod, Vector3 pos, Map m, int p, Color c)
            : base(sphere,proj, mod, pos,m,p,c)
        {
            maxHealth = 3000;
            health = maxHealth;
            baseDamage = 50;
            size = 0.01f;
            maxSpeed = 0.001f;


            this.sphere = sphere;

        }

        public override void update(GameTime gameTime)
        {




            refillHealth();
            base.update(gameTime);
        }

        public override void attack(GameTime gameTime)
        {

            Vector3 aim = target.position - this.position;
            float distance = aim.Length();
            aim.Normalize();

            float ang = (float)Math.Acos(Vector3.Dot(aim, this.direction));

            Vector3 cross = Vector3.Cross(aim, this.direction);
            if (cross.Y < 0)
                ang *= -1;

            if (ang < -0.1)
                direction = Vector3.Transform(direction, Matrix.CreateRotationY(0.1f));
            else if (ang > 0.1)
                direction = Vector3.Transform(direction, Matrix.CreateRotationY(-0.1f));



            //Console.WriteLine(direction);



            if (distance <= attackRange)
            {

                speed = 0;


                damageTimer += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
                //attack animation
                if (damageTimer >= attackTimer)
                {
                    damage = baseDamage + (float)rand.NextDouble() * (3*baseDamage);
                    target.health -= damage;
                    damageTimer = 0;
                }

                if (target.health <= 0)
                {
                    target = null;
                   
                    behaviour = AIState.PATH;
                }

            }

            else
            {
                speed = maxSpeed;
                this.direction.Normalize();

                this.velocity = this.direction * this.speed;
                this.position += this.velocity;

            }




           
        }

        public void refillHealth() 
        {
            if (health <= (maxHealth / 2))
            {
                current = path.Count - 1;
                target = null;
                behaviour = AIState.PATH;
                float dist = (position - path[path.Count - 1].position).Length();
                if (dist <= 0.05f)
                {
                    //effects.Update(gameTime);
                    health = maxHealth;
                }
                
            }
        }//end of refill
    }
}
